﻿Option Strict On
Imports s3pi.Interfaces
Imports s3pi.Package
Imports s3pi.WrapperDealer
Imports Microsoft.Win32
Imports System.Text
Imports System.IO



Public Class packInteraction


    Private Shared ReadOnly Property LocalName As String
        Get
            Dim registryKey As RegistryKey = Registry.LocalMachine.OpenSubKey("SOFTWARE\Sims\The Sims 3", False)
            Dim strLocal As String = registryKey.GetValue("Locale").ToString
            Return strLocal
        End Get
    End Property


    Public Shared ReadOnly Property ModFolder As String
        Get
            Dim docpath As String = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) & "\Electronic Arts", Locales.Item(LocalName))
            docpath = Path.Combine(docpath, "Mods")
            Return docpath
        End Get
    End Property


    ''' <summary>
    ''' This is to read DBC package in the document folder
    ''' </summary>
    ''' <value></value>
    ''' <returns>package</returns>
    ''' <remarks>Read dbcs and then extract casp and thum. NOTE: I use if to avoid small thums</remarks>
    Public Shared ReadOnly Property GetDBCpackage As IPackage

        Get
            
            Dim Folder As String = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) & "\Electronic Arts", Locales.Item(LocalName))
            Folder = Path.Combine(Folder, "DCCache")
            Dim pack As IPackage = Package.NewPackage(1)

            For Each filepathPack As String In My.Computer.FileSystem.GetFiles(Folder, FileIO.SearchOption.SearchAllSubDirectories, "*.dbc")
                Dim newPack As IPackage = Package.OpenPackage(1, filepathPack, False)
                'For Each entry As IResourceIndexEntry In newPack.GetResourceList
                '    GetEntry(entry, pack, newPack)

                'Next
                Dim type As UInt32() = New UInt32() {&H34AEECB, &H626F60CE}
                Dim Entries As IList(Of IResourceIndexEntry) = newPack.FindAll((Function(Entry As IResourceIndexEntry) type.Contains(Entry.ResourceType)))
                For Each entry As IResourceIndexEntry In Entries
                    pack.AddResource(entry, WrapperDealer.GetResource(1, newPack, entry).Stream, True)
                Next
            Next

            

            Dim thumbPath As String = Path.Combine(ModFolder.Replace("\Mods", ""), "Thumbnails\CASThumbnails.package")
            If IO.File.Exists(thumbPath) = True Then
                Dim ThumbnailPack As IPackage = Package.OpenPackage(1, thumbPath, False)
                Dim typeThumb As UInt32() = New UInt32() {&H626F60CE}
                Dim EntriesThumb As IList(Of IResourceIndexEntry) = ThumbnailPack.FindAll((Function(Entry As IResourceIndexEntry) typeThumb.Contains(Entry.ResourceType)))
                For Each entry As IResourceIndexEntry In EntriesThumb
                    pack.AddResource(entry, WrapperDealer.GetResource(1, ThumbnailPack, entry).Stream, True)
                Next
            End If


            Return pack
        End Get

    End Property

    ''' <summary>
    ''' Only for CASP Editor.
    ''' </summary>
    ''' <value></value>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Shared Property CustomPackPath As String

    ''' <summary>
    ''' This is to read  package in the package folder
    ''' </summary>
    ''' <value></value>
    ''' <returns>package</returns>
    ''' <remarks>Read packages and then extract casp and thum. NOTE: I use if to avoid small thums</remarks>
    Public Shared ReadOnly Property GetCustomPackage As IPackage
        Get
            Dim pack As IPackage = Package.NewPackage(1)
            For Each filepathPack As String In My.Computer.FileSystem.GetFiles(ModFolder, FileIO.SearchOption.SearchAllSubDirectories, "*.package")
                If filepathPack <> CustomPackPath Then
                    Dim newPack As IPackage = Package.OpenPackage(1, filepathPack, False)
                    'For Each entry As IResourceIndexEntry In newPack.GetResourceList
                    '    GetEntry(entry, pack, newPack)

                    'Next
                    Dim type As UInt32() = New UInt32() {&H34AEECB, &H626F60CE}
                    Dim Entries As IList(Of IResourceIndexEntry) = newPack.FindAll((Function(Entry As IResourceIndexEntry) type.Contains(Entry.ResourceType)))
                    For Each entry As IResourceIndexEntry In Entries
                        pack.AddResource(entry, WrapperDealer.GetResource(1, newPack, entry).Stream, True)
                    Next
                End If
            Next

            Dim thumbPath As String = Path.Combine(ModFolder.Replace("\Mods", ""), "Thumbnails\CASThumbnails.package")
            If IO.File.Exists(thumbPath) = True Then
                Dim ThumbnailPack As IPackage = Package.OpenPackage(1, thumbPath, False)
                Dim typeThumb As UInt32() = New UInt32() {&H626F60CE}
                Dim EntriesThumb As IList(Of IResourceIndexEntry) = ThumbnailPack.FindAll((Function(Entry As IResourceIndexEntry) typeThumb.Contains(Entry.ResourceType)))
                For Each entry As IResourceIndexEntry In EntriesThumb
                    pack.AddResource(entry, WrapperDealer.GetResource(1, ThumbnailPack, entry).Stream, True)
                Next
            End If


            Return pack
        End Get
    End Property

    ' ''' <summary>
    ' ''' This is the sub to re-use
    ' ''' </summary>
    ' ''' <param name="entry">IRourceIndexEntry</param>
    ' ''' <param name="pack">pack is to be added</param>
    ' ''' <param name="newPack">NewPack that is to be opened</param>
    ' ''' <remarks>Discarded</remarks>
    'Private Sub GetEntry(ByVal entry As IResourceIndexEntry, ByVal pack As IPackage, ByVal newPack As IPackage)
    '    If entry.ResourceType = &H34AEECB Then
    '        Dim _Stream As Stream = WrapperDealer.GetResource(1, newPack, entry, True).Stream
    '        pack.AddResource(entry, _Stream, False)
    '        Count += 1
    '    End If
    '    If entry.ResourceType = &H626F60CE Then
    '        Dim _Stream As Stream = WrapperDealer.GetResource(1, newPack, entry, True).Stream

    '        pack.AddResource(entry, _Stream, False)

    '    End If
    'End Sub

    ''' <summary>
    ''' This is the dictionary to generate the name
    ''' </summary>
    ''' <remarks>From MTS Wiki</remarks>
#Region "Local Name"
    Private Shared Locales As New Dictionary(Of String, String)() From { _
    {"en-US", Encoding.Unicode.GetString(New Byte() {&H54, &H0, &H68, &H0, &H65, &H0, _
        &H20, &H0, &H53, &H0, &H69, &H0, _
        &H6D, &H0, &H73, &H0, &H20, &H0, _
        &H33, &H0})}, _
    {"zh-CN", Encoding.Unicode.GetString(New Byte() {&H21, &H6A, &HDF, &H62, &HBA, &H4E, _
        &H1F, &H75, &H33, &H0})}, _
    {"zh-TW", Encoding.Unicode.GetString(New Byte() {&H21, &H6A, &HEC, &H64, &H2, &H5E, _
        &H11, &H6C, &H33, &H0})}, _
    {"cs-CZ", Encoding.Unicode.GetString(New Byte() {&H54, &H0, &H68, &H0, &H65, &H0, _
        &H20, &H0, &H53, &H0, &H69, &H0, _
        &H6D, &H0, &H73, &H0, &H20, &H0, _
        &H33, &H0})}, _
    {"da-DK", Encoding.Unicode.GetString(New Byte() {&H54, &H0, &H68, &H0, &H65, &H0, _
        &H20, &H0, &H53, &H0, &H69, &H0, _
        &H6D, &H0, &H73, &H0, &H20, &H0, _
        &H33, &H0})}, _
    {"nl-NL", Encoding.Unicode.GetString(New Byte() {&H44, &H0, &H65, &H0, &H20, &H0, _
        &H53, &H0, &H69, &H0, &H6D, &H0, _
        &H73, &H0, &H20, &H0, &H33, &H0})}, _
    {"fi-FI", Encoding.Unicode.GetString(New Byte() {&H54, &H0, &H68, &H0, &H65, &H0, _
        &H20, &H0, &H53, &H0, &H69, &H0, _
        &H6D, &H0, &H73, &H0, &H20, &H0, _
        &H33, &H0})}, _
    {"fr-FR", Encoding.Unicode.GetString(New Byte() {&H4C, &H0, &H65, &H0, &H73, &H0, _
        &H20, &H0, &H53, &H0, &H69, &H0, _
        &H6D, &H0, &H73, &H0, &H20, &H0, _
        &H33, &H0})}, _
    {"de-DE", Encoding.Unicode.GetString(New Byte() {&H44, &H0, &H69, &H0, &H65, &H0, _
        &H20, &H0, &H53, &H0, &H69, &H0, _
        &H6D, &H0, &H73, &H0, &H20, &H0, _
        &H33, &H0})}, _
    {"el-GR", Encoding.Unicode.GetString(New Byte() {&H54, &H0, &H68, &H0, &H65, &H0, _
        &H20, &H0, &H53, &H0, &H69, &H0, _
        &H6D, &H0, &H73, &H0, &H20, &H0, _
        &H33, &H0})}, _
    {"hu-HU", Encoding.Unicode.GetString(New Byte() {&H54, &H0, &H68, &H0, &H65, &H0, _
        &H20, &H0, &H53, &H0, &H69, &H0, _
        &H6D, &H0, &H73, &H0, &H20, &H0, _
        &H33, &H0})}, _
    {"it-IT", Encoding.Unicode.GetString(New Byte() {&H54, &H0, &H68, &H0, &H65, &H0, _
        &H20, &H0, &H53, &H0, &H69, &H0, _
        &H6D, &H0, &H73, &H0, &H20, &H0, _
        &H33, &H0})}, _
    {"ja-JP", Encoding.Unicode.GetString(New Byte() {&HB6, &H30, &H65, &HFF, &HB7, &H30, _
        &HE0, &H30, &HBA, &H30, &H13, &HFF})}, _
    {"ko-KR", Encoding.Unicode.GetString(New Byte() {&HEC, &HC2, &H88, &HC9, &H20, &H0, _
        &H33, &H0})}, _
    {"no-NO", Encoding.Unicode.GetString(New Byte() {&H54, &H0, &H68, &H0, &H65, &H0, _
        &H20, &H0, &H53, &H0, &H69, &H0, _
        &H6D, &H0, &H73, &H0, &H20, &H0, _
        &H33, &H0})}, _
    {"pl-PL", Encoding.Unicode.GetString(New Byte() {&H54, &H0, &H68, &H0, &H65, &H0, _
        &H20, &H0, &H53, &H0, &H69, &H0, _
        &H6D, &H0, &H73, &H0, &H20, &H0, _
        &H33, &H0})}, _
    {"pt-PT", Encoding.Unicode.GetString(New Byte() {&H4F, &H0, &H73, &H0, &H20, &H0, _
        &H53, &H0, &H69, &H0, &H6D, &H0, _
        &H73, &H0, &H20, &H0, &H33, &H0})}, _
    {"pt-BR", Encoding.Unicode.GetString(New Byte() {&H54, &H0, &H68, &H0, &H65, &H0, _
        &H20, &H0, &H53, &H0, &H69, &H0, _
        &H6D, &H0, &H73, &H0, &H20, &H0, _
        &H33, &H0})}, _
    {"ru-RU", Encoding.Unicode.GetString(New Byte() {&H54, &H0, &H68, &H0, &H65, &H0, _
        &H20, &H0, &H53, &H0, &H69, &H0, _
        &H6D, &H0, &H73, &H0, &H20, &H0, _
        &H33, &H0})}, _
    {"es-ES", Encoding.Unicode.GetString(New Byte() {&H4C, &H0, &H6F, &H0, &H73, &H0, _
        &H20, &H0, &H53, &H0, &H69, &H0, _
        &H6D, &H0, &H73, &H0, &H20, &H0, _
        &H33, &H0})}, _
    {"es-MX", Encoding.Unicode.GetString(New Byte() {&H54, &H0, &H68, &H0, &H65, &H0, _
        &H20, &H0, &H53, &H0, &H69, &H0, _
        &H6D, &H0, &H73, &H0, &H20, &H0, _
        &H33, &H0})}, _
    {"sv-SE", Encoding.Unicode.GetString(New Byte() {&H54, &H0, &H68, &H0, &H65, &H0, _
        &H20, &H0, &H53, &H0, &H69, &H0, _
        &H6D, &H0, &H73, &H0, &H20, &H0, _
        &H33, &H0})}, _
    {"th-TH", Encoding.Unicode.GetString(New Byte() {&H40, &HE, &H14, &HE, &H2D, &HE, _
        &H30, &HE, &HB, &HE, &H34, &HE, _
        &H21, &HE, &H2A, &HE, &H4C, &HE, _
        &H20, &H0, &H33, &H0})} _
}
#End Region


    ''' <summary>
    ''' Test function 
    ''' </summary>
    ''' <param name="stream"></param>
    ''' <returns>bytes</returns>
    ''' <remarks></remarks>
    Public Shared Function CtypeStreamtobytes(ByVal stream As Stream) As Byte()
        Dim bytes(CInt(stream.Length)) As Byte
        stream.Position = 0
        stream.Read(bytes, 0, CInt(stream.Length))
        Dim Data As String = Encoding.ASCII.GetString(bytes)
        Return bytes
    End Function


    ''' <summary>
    ''' This is to get the key arraylist in registry
    ''' </summary>
    ''' <value></value>
    ''' <returns>A arraylist containing keys that users installed</returns>
    ''' <remarks></remarks>
    Private Shared ReadOnly Property GetPack As ArrayList
        Get
            Dim KeyPack As New ArrayList
            For Each Name As String In PackCollection
                Dim key As RegistryKey = Registry.LocalMachine.OpenSubKey("SOFTWARE\Sims\" & Name, False)
                If InStr(key.Name, "Create") = 0 Then
                    KeyPack.Add(key)
                End If

            Next
            Return KeyPack
        End Get
    End Property

    ''' <summary>
    ''' This is to get the names of packs that users install in their computer
    ''' </summary>
    ''' <value>icoolection</value>
    ''' <returns>It will return the collection containing pack names </returns>
    ''' <remarks></remarks>
    Public Shared ReadOnly Property PackCollection As ICollection
        Get
            Dim listPack As New ArrayList
            For Each Name As String In Registry.LocalMachine.OpenSubKey("SOFTWARE\Sims", False).GetSubKeyNames
                If InStr(Name, "Create") = 0 Then
                    listPack.Add(Name)
                End If

            Next
            Return listPack
        End Get
    End Property

    ''' <summary>
    ''' This will get the path that comes from registry
    ''' </summary>
    ''' <value></value>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Private Shared ReadOnly Property PackPath As ICollection
        Get
            Dim listPack As New ArrayList
            For Each NameKey As RegistryKey In GetPack
                Dim strPath As String = NameKey.GetValue("Install Dir").ToString
                listPack.Add(strPath)
            Next
            Return listPack
        End Get
    End Property

    ''' <summary>
    ''' This part is to get the fuffbuild and thum in the game folder.
    ''' </summary>
    ''' <param name="name">The name of the pack</param>
    ''' <value>string</value>
    ''' <returns>It will return a package</returns>
    ''' <remarks>it set up the proper pack unless EA change its way</remarks>
    Public Shared ReadOnly Property GetEAstuff(ByVal name As String) As IPackage
        Get
            Dim n As Integer = CType(PackCollection, ArrayList).IndexOf(name)
            Dim Folder As String = CType(PackPath, ArrayList).Item(n).ToString
            Dim newPack As IPackage = Package.NewPackage(1)

            Dim PackCASP As IPackage = Nothing
            Dim PackThum As IPackage = Nothing
            If n = 0 Then
                PackCASP = Package.OpenPackage(1, Path.Combine(Folder, "GameData\Shared\Packages\FullBuild0.package"), False)
                PackThum = Package.OpenPackage(1, Path.Combine(Folder, "Thumbnails\CasThumbnails.package"), False)
            Else
                Dim strpath As String = Path.Combine(Folder, "GameData\Shared\Packages")
                Dim FullPack As String = My.Computer.FileSystem.GetFiles(strpath, FileIO.SearchOption.SearchAllSubDirectories, "*.package").Item(0)
                PackCASP = Package.OpenPackage(1, FullPack, False)


                PackThum = Package.OpenPackage(1, Path.Combine(Folder, "Thumbnails\CasThumbnails.package"), False)
            End If




            Dim typeCasp As UInt32() = New UInt32() {&H34AEECB}
            Dim typeThum As UInt32() = New UInt32() {&H626F60CE}
            Dim EntriesCasp As IList(Of IResourceIndexEntry) = PackCASP.FindAll((Function(Entry As IResourceIndexEntry) typeCasp.Contains(Entry.ResourceType)))
            Dim EntriesThum As IList(Of IResourceIndexEntry) = PackThum.FindAll((Function(Entry As IResourceIndexEntry) typeThum.Contains(Entry.ResourceType)))

            For Each entry As IResourceIndexEntry In EntriesCasp
                newPack.AddResource(entry, WrapperDealer.GetResource(1, PackCASP, entry, True).Stream, True)
            Next
            For Each entry As IResourceIndexEntry In EntriesThum
                newPack.AddResource(entry, WrapperDealer.GetResource(1, PackThum, entry, True).Stream, True)
            Next



            'set the count of casp
            Count = EntriesCasp.Count


            Return newPack
        End Get
    End Property

    ''' <summary>
    ''' Obsolete. No use now.
    ''' </summary>
    ''' <value></value>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Shared Property Count As Integer

End Class
